package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.dto.PositionGroupAuthDto;
import cn.com.cifi.mars.bean.dto.UserRoleDto;
import cn.com.cifi.mars.bean.vo.sys.rsp.PositionGroupAuthVo;
import cn.com.cifi.mars.bean.vo.sys.rsp.UserRoleVo;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.GenderEunm;
import cn.com.cifi.mars.constant.IsDisableEnum;
import cn.com.cifi.mars.constant.JobDefinitionTypeEnum;
import cn.com.cifi.mars.constant.UserTypeEunm;
import cn.com.cifi.mars.entity.common.SysUserRole;
import cn.com.cifi.mars.mapper.mars.SysUserRoleMapper;
import cn.com.cifi.mars.service.SysUserRoleService;
import cn.com.cifi.mars.util.UUIDUtils;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-18
 */
@Slf4j
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	
	/**
	 * 分配用户分页
	 */
	@Override
	public UserRoleVo queryUserRolePage(String current,String pageSize,String roleId) {
		Page<UserRoleDto>pageDto=new Page<UserRoleDto>();
		//参数当前页
		pageDto.setCurrent(Integer.parseInt(current));
		//参数每页数量
		pageDto.setSize(Integer.parseInt(pageSize));
		List<UserRoleDto>list=sysUserRoleMapper.selectUserRolePage(pageDto, roleId);
		list.forEach(x->{
			//性别编码 1：男 2：女
			String gender_id="";
			if(x.getGenderId()!=null) {
				gender_id=x.getGenderId();
				if(gender_id.equals(GenderEunm.FEMALE.getKey())) {
					gender_id=GenderEunm.FEMALE.getValue();
				}else {
					gender_id=GenderEunm.MALE.getValue();
				}
			}
			
			//用户类型//E1内部用户，E2 编外员工，O1外部用户，O2外部合作人员，O3供应商人员，O4代理商人员 
			String usertype=x.getUsertype();
			if(x.getUsertype()!=null) {
				if(usertype.equals(UserTypeEunm.E1.getKey())) {
					usertype=UserTypeEunm.E1.getValue();
				}else if(usertype.equals(UserTypeEunm.E2.getKey())) {
					usertype=UserTypeEunm.E2.getValue();
				}else if(usertype.equals(UserTypeEunm.O1.getKey())) {
					usertype=UserTypeEunm.O1.getValue();
				}else if(usertype.equals(UserTypeEunm.O2.getKey())) {
					usertype=UserTypeEunm.O2.getValue();
				}else if(usertype.equals(UserTypeEunm.O3.getKey())) {
					usertype=UserTypeEunm.O3.getValue();
				}else if(usertype.equals(UserTypeEunm.O4.getKey()))  {
					usertype=UserTypeEunm.O4.getValue();
				}else {
					usertype="未知";
				}
			}
			x.setGenderId(gender_id);
			x.setUsertype(usertype);
		});
		//分页和list
		UserRoleVo vo=new UserRoleVo();
		vo.setCurrent(pageDto.getCurrent());
		vo.setPages(pageDto.getPages());
		vo.setPageSize(pageDto.getSize());
		vo.setTotal(pageDto.getTotal());
		vo.setList(list);
		return vo;
	}

	/**
	 * 删除用户角色
	 */
	@Transactional
	@Override
	public int deleteUserRole(String id) {
		return sysUserRoleMapper.deleteById(id);
	}
	
	/**
	 * 权限管理-角色用户选择
	 */
	@Transactional
	@Override
	public int createUserRole(List<String> userIds,String roleId,String name) {
		if(userIds.size()==0) {
			throw new MsgException(CodeEnum.USER_ROLE_ID_ERROR);
		}
		//查询用户角色是否重复存在
		int count=sysUserRoleMapper.selectUserRoleByUserRoleId(roleId, userIds);
		if(count>0) {
			throw new MsgException(CodeEnum.USER_ROLE_ERROR);
		}
		int num=0;
		for (String string : userIds) {
			//开始添加
			SysUserRole bean=new SysUserRole();
			bean.setCreated(LocalDateTime.now());
			bean.setRoleId(roleId);
			bean.setUserId(string);
			bean.setCreateBy(name);
			bean.setId(UUIDUtils.create());
			num=sysUserRoleMapper.insert(bean);
		}
		return num;
	}

	/**
	 * 查询用户拥有多少角色
	 */
	@Override
	public UserRoleVo getUserRoleList(String userId) {
		List<UserRoleDto>list=sysUserRoleMapper.selectUserRoleList(userId);
		UserRoleVo vo=new UserRoleVo();
		vo.setList(list);
		return vo;
	}
}
